import { IState, QUI_Container, QUI_Group } from "../common.js";
import { MainWin } from "../mainwin.js";
import { GameResource } from "../resource/gameresource.js";
import { QUI_Map } from "../ui/map.js";

export class State_Main implements IState<MainWin> {
    container: QUI_Container;
    main: MainWin;

    panel_info: QUI_Group;
    panel_action: QUI_Group;
    panel_main: QUI_Group;
    OnInit(mgr: MainWin): void {
        this.main = mgr;
        this.container = new QUI_Container();
        mgr.canvas.InsertChild(this.container);//插在前面

        this.panel_info = new QUI_Group();
        this.panel_info.title.text = "信息区"
        this.panel_info.localRect.radioX2 = 0.25;
        this.container.AddChild(this.panel_info);

        this.panel_main = new QUI_Group();
        this.panel_main.title.text = "主图区"
        this.panel_main.localRect.radioX1 = 0.25;
        this.panel_main.localRect.radioY2 = 0.75;
        this.container.AddChild(this.panel_main);


        this.panel_action = new QUI_Group();
        this.panel_action.title.text = "交互区"
        this.panel_action.localRect.radioX1 = 0.25;
        this.panel_action.localRect.radioY1 = 0.75;
        this.container.AddChild(this.panel_action);

        //添加组件后 container 可能错过update，手动更新一次
        this.timer = 0;
        this.container.localColor.A = 0;
        this.container.OnUpdate(mgr.canvas, 0);

        this.InitAsync();
    }
    async InitAsync() {
        let map = new QUI_Map();
        this.panel_main.container.AddChild(map);
        let gmap = await GameResource.LoadMap(this.main.config.playerdata.position.map);
        map.LoadMap(gmap);
        let grid = gmap.grids[this.main.config.playerdata.position.area];
        map.SetPos(grid);
    }
    timer: number = 0;
    OnUpdate(delta: number): void {
        this.timer += delta;
        let alpha = 0;
        if (this.timer < 1)
            alpha = this.timer;
        else
            alpha = 1;

        this.container.localColor.A = alpha;

    }
    OnExit(): void {
        this.main.canvas.RemoveChild(this.container);
    }
    OnResize(width: number, height: number): void {

    }
    OnKey(keycode: string, press: boolean): void {

    }
    OnPointAfterGUI(id: number, x: number, y: number, press: boolean, move: boolean): void {

    }
    OnWheelAfterGUI(dx: number, dy: number, dz: number): void {

    }

}